# -*- coding: utf-8 -*

# work fine with pandoc 1.17.1

from subprocess import call
import os
from glob import glob
import sys
import codecs
import re
from collections import OrderedDict
from HTMLParser import HTMLParser
import json

folder = os.getcwd().decode('mbcs')


#trim the shall panel 
def preprocessHTML():
    htmlfiles = glob(folder+u"\\*.html")
    commands = []
    for htmlfile in htmlfiles:
        splits = htmlfile.split('\\')
        filename  = splits[len(splits)-1]
        f =   open(filename, 'r')
        content = f.read()
        content = re.sub('.*jp-post-flair.*','</div>',content)
        f.close()
        f =   open(filename, 'w')
        f.write(content)
        f.close()

#convert the math section
def processMath():
    htmlfiles = glob(folder+u"\\*.html")
    commands = []
    for htmlfile in htmlfiles:
        splits = htmlfile.split('\\')
        filename  = splits[len(splits)-1] 
        f =   open(filename, 'r')
        content = f.read()
        replacedStr = re.sub('<img.*?class="latex">', _convertMath, content);
        f.close()
        f =   open(filename, 'w')
        f.write(replacedStr)
        f.close()

def _convertMath(matched):
    text  = matched.group(0)
    texnote = re.search('(?<=alt=").*?(?=")',text).group(0)
    h     = HTMLParser()
    text1 = h.unescape(texnote).encode('utf-8')
    text  = '<span class="math inline">$' + text1 +'$</span>'
    return text

def processHTML():
    htmlfiles = glob(folder+u"\\*.html")
    commands = []
    for htmlfile in htmlfiles:
        splits = htmlfile.split('\\')
        filename  = splits[len(splits)-1]
        corrspondFileName = filename.split('.')[0] + '.texSection'
        command = u'pandoc -f html -t latex '+u'"'+ filename +u'"' +u' -o ' + u'"'+ corrspondFileName +u'"'  
        # print command.encode('mbcs')
        os.system(command.encode('mbcs'))

def addSection():
    texfiles = glob(folder+u"\\*.texSection")
    
    labelWriting = 'onwriting'
    labelCareer = 'careeradvice'

    commands = []
    for texfile in texfiles:
        splits = texfile.split('\\')
        filename  = splits[len(splits)-1]
        # file = codecs.open(filename, 'r+', encoding='utf-8')
        line = filename.split('.')[0]
        name = line.replace('_','"')
        # label  = re.sub('[\s+]','', line).replace('\'','').replace('_','').lower()
        label  = re.sub('\W','', line).replace('_','').lower()
        labela = label.encode('utf-8')
        if (labela != labelWriting) & (labela != labelCareer):
            line = u'\subsection{'+name +u'} \n' + '\label{sec:' + label +u'}\n\n'
        else:
            line = u'\section{'+name +u'} \n' + '\label{sec:' + label +u'}\n\n'
        # file.writelines(line)
        # file.close()
        # with file(filename, 'r') as original: 
        #     data = original.read()
        # with file(filename, 'w') as modified: 
        #     modified.write(line + data)

        line_prepender(filename,line)



# reprocess Math 
def reprocessLatexMath():
    texfiles = glob(folder+u"\\*.texSection")
    commands = []
    for texfile in texfiles:
        splits = texfile.split('\\')
        filename  = splits[len(splits)-1]
        f =   open(filename, 'r')
        content = f.read()
        
        content1 = re.sub(r'\{\\\$', r'$', content)
        # re.search(r'\{\\\$',  content)
        content1 = re.sub(r'\\\$\}', r'$', content1)
        # content1 = re.sub(r'\\\$', r'\$', content1)
        content1 = re.sub(r'\\textbackslash\{\}', r'\\', content1)
        content1= re.sub(r'\grp','\\\\grp',content1)
        content1 = re.sub(r'\\\{','{',content1)
        content1 = re.sub(r'\\\}','}',content1)
        content1 = re.sub(r'\\frac(?!{)',r'\\\\frac',content1)

        f.close()
        f =   open(filename, 'w')
        f.write(content1)
        f.close()

#section highperlink 
def reprocessLatexSectionLink():
    texfiles = glob(folder+u"\\*.texSection")
    commands = []

    for texfile in texfiles:
        splits = texfile.split('\\')
        filename  = splits[len(splits)-1]
        f =   open(filename, 'r')

        content = f.read()
        content1 = re.sub(r'\\href\{https:.*?\}', _replaceSectionLink , content)

        f.close()
        f =   open(filename, 'w')
        f.write(content1)
        f.close()


def _replaceSectionLink(matched):
    texfiles = glob(folder+u"\\*.texSection")
    sectionName =[]
    for texfile in texfiles:
        splits = texfile.split('\\')
        filename  = splits[len(splits)-1]

        line = filename.split('.')[0]
        # label  = re.sub('[\s+]','', line).replace('\'','').replace('_','').lower()
        label  = re.sub('\W','', line).replace('_','').lower()
        sectionName.append(label)
    text  = matched.group(0)
    #this should be modified if have some change `
    sectionText =  re.search(r'(?<=advice-on-writing-papers/).*?(?=\})',text)
    if sectionText == None:
        sectionText =  re.search(r'(?<=career-advice/).*?(?=\})',text)
        if sectionText == None:
            sectionText =''
        else:
            sectionText = sectionText.group(0)
    else:
        sectionText = sectionText.group(0)
    if len(sectionText)!= 0:
        sectionText = sectionText.replace('-','').lower().replace('\\%e2\\%80\\%9c','').replace('\\%e2\\%80\\%9d','').replace('/','')
        if sectionText in sectionName:
            label =  'sec:' + sectionText
            returnText  = r'\hyperref['+label+']'
            text  = returnText 
            pass
        pass
    return text
    


def line_prepender(filename, line):
    with open(filename, 'r+') as f:
        content = f.read()
        f.seek(0, 0)
        contents = line.rstrip(u'\r\n') + u'\n'
        contents= contents.encode('utf-8')
        f.write(contents + content)


def finalTex():

    labelWriting = 'onwriting'
    labelCareer = 'careeradvice'


    filename = '1.texTmp'
    filefinal = 'final.tex'

    fileLevel  = 'FileLevel.json'

    f = open(fileLevel,'r')
    fileLevelData =  json.load(f)
    f.close()

    f  = open (filename,'r')
    texHeader = f.read()
    f.close()
    texEnd = '\end{document}'

    # list order represents the depth
    contentsOnWriting = []
    contentsCareer   =[]
    texfiles = glob(folder+u"\\*.texSection")

    for texfile in texfiles:
        splits = texfile.split('\\')
        filename  = splits[len(splits)-1]
        f=open(filename,'r')
        content = f.read()
        f.close

        line = filename.split('.')[0]
        # label  = re.sub('[\s+]','', line).replace('\'','').replace('_','').lower()
        label  = re.sub('\W','', line).replace('_','').lower()
        labela = label.encode('utf-8')
        if (labela != labelWriting) & (labela != labelCareer):
            depth =  fileLevelData[label]['depth']

            if  fileLevelData[label]['type'] == u'advice-on-writing-papers':
                if len(contentsOnWriting) < depth +1:
                    contentsOnWriting  =  listSize(depth +1,contentsOnWriting )
                contentsOnWriting[depth] = contentsOnWriting[depth] + '\n' +content +'\n'
            else:
                if len(contentsCareer) < depth +1:
                    contentsCareer  =  listSize(depth +1,contentsCareer )
                contentsCareer[depth] = contentsCareer[depth] + '\n' +content +'\n'           
        elif labela == labelWriting:
            writingContent = '\n' +content +'\n'
        else:
            CareerContent  = '\n' +content +'\n'
        #contents  = contents+'\n' +content +'\n'
    
    contents = writingContent

    for content in contentsOnWriting:
        contents = contents + content
        
    contents  = contents+ CareerContent
    for content in contentsCareer:
        contents = contents + content   

    contents = texHeader +'\n' +contents +'\n'+ texEnd

    f = open(filefinal,'w')

    f.write(contents)

    f.close()


def listSize(length,listIn=None):
    if listIn is None:
        listIn = []
    appendCount = length - len(listIn)
    for i in range(appendCount):
        listIn.append('')
    return listIn

def main():
    preprocessHTML()
    processMath()
    processHTML()
    addSection()
    reprocessLatexMath()
    reprocessLatexSectionLink()
    finalTex()


if __name__ =='__main__':
    main()